//
//  SSRegisteInPutView.m
//  textDemo
//
//  Created by Esther on 2017/2/18.
//  Copyright © 2017年 Esther. All rights reserved.
//

#import "SSRegisteInPutView.h"

@interface SSRegisteInPutView()<UITextFieldDelegate>
@property(nonatomic, strong) UILabel *tostLabel;                // 提示Label
@property(nonatomic, strong) UITextField *userNameTextField;    // 用户名
@property(nonatomic, strong) UITextField *passwordTextField;    // 密码
@property(nonatomic, strong) UIButton *nextButton;              // 下一步

@property(nonatomic, strong) UIButton *goLoginButton;           // 去登陆
@property(nonatomic, strong) UILabel *textBackLabel;            // 输入框背景
@property(nonatomic, strong) UILabel *textLineLabel;            // 输入框背景中间的分割线

@end

@implementation SSRegisteInPutView

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
    }
    return self;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self addSubview:self.tostLabel];
    [self addSubview:self.textBackLabel];
    [self addSubview:self.userNameTextField];
    [self addSubview:self.passwordTextField];
    [self addSubview:self.textLineLabel];
    [self addSubview:self.nextButton];
    [self addSubview:self.goLoginButton];
    
    #pragma mark - 约束
    __weak typeof (self) weakSelf = self;
    [_tostLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(35);
        make.top.right.equalTo(weakSelf);
        make.left.equalTo(weakSelf.mas_left).offset(15);
    }];
    
    [_textBackLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(89);
        make.top.equalTo(weakSelf.tostLabel.mas_bottom);
        make.left.equalTo(weakSelf.mas_left).offset(-1);
        make.right.equalTo(weakSelf.mas_right).offset(1);
    }];
    
    [_userNameTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.mas_equalTo(44);
        make.top.equalTo(weakSelf.textBackLabel.mas_top);
    }];
    
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.mas_equalTo(44);
        make.top.equalTo(weakSelf.userNameTextField.mas_bottom).offset(1);
    }];
    
    [_textLineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(1);
        make.top.equalTo(weakSelf.userNameTextField.mas_bottom);
        make.left.equalTo(weakSelf.textBackLabel.mas_left).offset(15);
        make.right.equalTo(weakSelf.textBackLabel.mas_right).offset(-15);
    }];
    
    [_nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(16);
        make.right.equalTo(weakSelf.mas_right).offset(-16);
        make.height.mas_equalTo(35);
        make.top.equalTo(weakSelf.passwordTextField.mas_bottom).offset(15);
    }];
    
    [_goLoginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(50.0, 16.0));
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.top.equalTo(weakSelf.nextButton.mas_bottom).offset(23);
    }];
}

#pragma mark - textFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    if (textField == self.userNameTextField && range.location == 11) {
        return NO;
    }
    return YES;
}

#pragma mark -
#pragma mark - TextField监听值改变

- (void)userNameTextFieldChange:(UITextField *)textField{
    
    NSLog(@"userNameTextFieldChange,%zd",textField.text.length);
    if (textField.text.length == 11 && self.passwordTextField.text.length > 5) {
        self.nextButton.backgroundColor = [UIColor ss_colorWithRed:56 green:165 blue:241];
        _nextButton.enabled = YES;
        
    }else{
        self.nextButton.backgroundColor = [UIColor ss_colorWithRed:229 green:229 blue:229];
        _nextButton.enabled = NO;
        
    }
    
}

- (void)passwordTextFieldChange:(UITextField *)textField{
    NSLog(@"passwordTextFieldChange%zd",textField.text.length);
    if (textField.text.length > 5 && self.userNameTextField.text.length == 11) {
        self.nextButton.backgroundColor = [UIColor ss_colorWithRed:56 green:165 blue:241];
        _nextButton.enabled = YES;
    }else{
        self.nextButton.backgroundColor = [UIColor ss_colorWithRed:229 green:229 blue:229];
        _nextButton.enabled = NO;
    }
    
}

- (void)pushNextController{
    if (self.nextControllerBlock) {
        self.nextControllerBlock(@{@"userName" : _userNameTextField.text,
                                   @"password" : _passwordTextField.text
                                   });
    }
}

#pragma mark -
#pragma mark - 懒加载

- (UILabel *)tostLabel{
    if (!_tostLabel) {
        _tostLabel = [[UILabel alloc] init];
        _tostLabel.text = @"请输入手机号码注册新用户";
        _tostLabel.font = [UIFont systemFontOfSize:14.0];
        _tostLabel.textColor = [UIColor ss_colorWithRed:81 green:81 blue:81];
        
    }
    return _tostLabel;
}

- (UILabel *)textBackLabel{
    if (!_textBackLabel) {
        _textBackLabel = [[UILabel alloc] init];
        _textBackLabel.backgroundColor = [UIColor whiteColor];
        _textBackLabel.layer.borderWidth = 1;
        _textBackLabel.layer.borderColor = [UIColor ss_colorWithRed:188 green:188 blue:188].CGColor;
    }
    return _textBackLabel;
}

- (UITextField *)userNameTextField{
    if (!_userNameTextField) {
        _userNameTextField = [[UITextField alloc] init];
        _userNameTextField.delegate = self;
        _userNameTextField.placeholder = @"请输入手机号码";
        _userNameTextField.keyboardType = UIKeyboardTypeNumberPad;
        
        [_userNameTextField addTarget:self action:@selector(userNameTextFieldChange:) forControlEvents:UIControlEventAllEditingEvents];
        
        _userNameTextField.text = @"13918473104";
        
        
    }
    return _userNameTextField;
}

- (UITextField *)passwordTextField{
    if (!_passwordTextField) {
        _passwordTextField = [[UITextField alloc] init];
        _passwordTextField.delegate = self;
        _passwordTextField.placeholder = @"设置账号密码";
        _passwordTextField.secureTextEntry = YES;
        [_passwordTextField addTarget:self action:@selector(passwordTextFieldChange:) forControlEvents:UIControlEventAllEditingEvents];
        _passwordTextField.text = @"123456";
        
    }
    return _passwordTextField;
}

- (UILabel *)textLineLabel{
    if (!_textLineLabel) {
        _textLineLabel = [[UILabel alloc] init];
        _textLineLabel.backgroundColor = [UIColor ss_colorWithRed:188 green:188 blue:188];
    }
    return _textLineLabel;
}

- (UIButton *)nextButton{
    if (!_nextButton) {
        _nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_nextButton setTitle:@"下一步" forState:UIControlStateNormal];
        _nextButton.backgroundColor = [UIColor ss_colorWithRed:229 green:229 blue:229];
        [_nextButton setTitleColor:[UIColor ss_colorWithRed:132 green:132 blue:132] forState:UIControlStateNormal];
        [_nextButton addTarget:self action:@selector(pushNextController) forControlEvents:UIControlEventTouchUpInside];
        _nextButton.enabled = NO;
    }
    return _nextButton;
}

- (UIButton *)goLoginButton{
    if (!_goLoginButton) {
        _goLoginButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_goLoginButton setTitleColor:[UIColor ss_colorWithRed:56 green:145 blue:241] forState:UIControlStateNormal];
        [_goLoginButton setTitle:@"去登录" forState:UIControlStateNormal];
        _goLoginButton.backgroundColor = [UIColor ss_colorWithRed:242 green:242 blue:242];
        _goLoginButton.titleLabel.font = [UIFont systemFontOfSize:16.0f];
    }
    return _goLoginButton;
}


@end
